package com.framework.view.indicator;

import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.framework.view.indicator.configs.GlobalConfig;
import com.framework.view.indicator.indicators.configs.AbstractIndicatorConfig;
import com.framework.view.indicator.listener.OnScrollResultListener;

/**
 * 指示器的适配器
 *
 * @author liangzx
 * @version 1.0
 * @time 2019-12-14 17:11
 **/
public abstract class BaseIndicatorAdapter<K extends AbstractIndicatorConfig> {

    private final DataSetObservable mDataSetObservable = new DataSetObservable();
    /**
     * 滑动结果监听
     */
    private OnScrollResultListener mOnScrollResultListener;
    /**
     * 滑动页控件
     */
    protected ViewGroup mViewPager;

    public BaseIndicatorAdapter() {
    }

    /**
     * 自动绑定viewpager，获取页面数量及标题
     *
     * @param viewPager
     */
    public BaseIndicatorAdapter(ViewPager viewPager) {
        mViewPager = viewPager;
    }

    /**
     * 自动绑定viewpager2，获取页面数量
     *
     * @param viewPager
     */
    public BaseIndicatorAdapter(ViewPager2 viewPager) {
        mViewPager = viewPager;
    }

    /**
     * 返回数量
     *
     * @return 返回数量
     */
    public int getCount() {
        if (mViewPager instanceof ViewPager2) {
            RecyclerView.Adapter adapter = ((ViewPager2) mViewPager).getAdapter();
            return null == adapter ? 0 : adapter.getItemCount();
        } else if (mViewPager instanceof ViewPager) {
            PagerAdapter adapter = ((ViewPager) mViewPager).getAdapter();
            return null == adapter ? 0 : adapter.getCount();
        }
        return 0;
    }

    /**
     * 绑定全局配置
     *
     * @param config
     * @return
     */
    protected void bindGlobalConfig(GlobalConfig config) {
    }

    /**
     * 绑定指示器配置
     *
     * @return
     */
    protected abstract K bindIndicatorConfig();

    /**
     * 注册数据监听
     *
     * @param observer
     */
    final void registerDataSetObserver(DataSetObserver observer) {
        mDataSetObservable.registerObserver(observer);
    }

    /**
     * 注销数据监听
     *
     * @param observer
     */
    final void unregisterDataSetObserver(DataSetObserver observer) {
        mDataSetObservable.unregisterObserver(observer);
    }

    /**
     * 通知数据更新
     */
    public final void notifyDataSetChanged() {
        mDataSetObservable.notifyChanged();
    }

    /**
     * 获取指示器配置<br>
     * 返回空则不显示指示器
     *
     * @return
     */
    final K getIndicatorConfig() {
        return bindIndicatorConfig();
    }

    /**
     * 获取全局配置
     *
     * @return
     */
    GlobalConfig getGlobalConfig() {
        GlobalConfig factory = GlobalConfig.factory();
        bindGlobalConfig(factory);
        return factory;
    }

    /**
     * 绑定监听器
     *
     * @return
     */
    public void setOnScrollResultListener(OnScrollResultListener listener) {
        mOnScrollResultListener = listener;
    }

    /**
     * 获取监听
     *
     * @return
     */
    OnScrollResultListener getListener() {
        return mOnScrollResultListener;
    }

    /**
     * 获取ViewPager或ViewPager2
     *
     * @return
     */
    public ViewGroup getViewPager() {
        return mViewPager;
    }

    /**
     * 反向绑定viewpager
     *
     * @param viewPager
     */
    public BaseIndicatorAdapter bindViewPager(ViewPager viewPager) {
        if (mViewPager != viewPager) {
            mViewPager = viewPager;
            mDataSetObservable.notifyInvalidated();
        }
        return this;
    }

    /**
     * 反向绑定viewpager2
     *
     * @param viewPager
     */
    public BaseIndicatorAdapter bindViewPager(ViewPager2 viewPager) {
        if (mViewPager != viewPager) {
            mViewPager = viewPager;
            mDataSetObservable.notifyInvalidated();
        }
        return this;
    }

}
